#include "GamePlayState.h"
#include "ResourceManager.h"
#include "Enemy.h"
#include<time.h>

void CGamePlayState::Init()
{
	this->m_background  = CResourceManager::GetInstance()->GetSprite(RESOURCE__GAMEPLAY_BACKGROUND);
	m_mario->Init();
	m_map1 = CResourceManager::GetInstance()->GetMap(RESOURCE__GAMEPLAY_MAP);
	objectList = m_map1->GetObjects();
	for (int i = 0; i < 1; i++)
	{
		CEnemy* enemy = new CEnemy(D3DXVECTOR2(rand() % 700, 350));
		enemy->Init();
		objectList2.push_back(enemy);
	}
	m_mario->Init();
	m_camera = new CCamera();
}

void CGamePlayState::Update(float gameTime, CInput* input)
{
	m_physicalSystem->GetSweptBroadphaseBox(m_mario);
	for (int i = 0; i < objectList.size(); i++)
	{
		/*if (m_mario->IsCollideWith(objectList[i]))
		{
			m_physicalSystem->TwoObject(m_mario, objectList[i], gameTime, input);
		}*/
		m_physicalSystem->MoveAABB(m_mario,objectList[i],gameTime,input);
	}

	//m_mario->Update(gameTime, input);

	/*for (int i = 0; i < objectList2.size(); i++)
	{
		objectList2[i]->Update(gameTime, input);
	}
	for (int i = 0; i < objectList.size(); i++)
	{
		objectList[i]->Update(gameTime, input);
	}
*/
	bool isColi = false;
	
	for (int i = 0; i < objectList2.size(); i++)
	{
		CObject* temp = objectList2[i];
		for (int j = 0; j < objectList.size(); j++)
		{
			if (temp->IsCollideWith(objectList[j]))
			{
				m_physicalSystem->TwoObject(temp, objectList[j], gameTime, input);
			}
		}
	}
	
	m_camera->Update(m_mario->GetPosition());
}

void CGamePlayState::Draw(LPD3DXSPRITE spriteHandler)
{
	m_mario->Draw(spriteHandler, m_camera);

	//this->m_background->Draw(spriteHandler);
	for (int i = 0; i < objectList.size(); i++)
	{
		objectList[i]->Draw(spriteHandler, m_camera);
	}
	for (int i = 0; i < objectList2.size(); i++)
	{
		objectList2[i]->Draw(spriteHandler, m_camera);
	}
}

void CGamePlayState::LoadObjectFromMap(CMap* map)
{

}